package com.lh.common.utils;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.security.MessageDigest;


/**  
 * @Description: 加密方法
 * @author TangCai
 * @date 2016年11月3日 上午11:00:27
 * @version V1.0  
 */
@SuppressWarnings("restriction")
public class MD5Encrypt {

    private static final String UTF8 = "utf-8";  
  
    /** 
     * MD5数字签名 (大)
     * @param src 
     * @return 
     * @throws Exception 
     */  
    public static String md5Digest(String src) throws Exception {  
       // 定义数字签名方法, 可用：MD5, SHA-1  
       MessageDigest md = MessageDigest.getInstance("MD5");  
       byte[] b = md.digest(src.getBytes(UTF8));  
       return byte2HexStr(b);  
    }  
    /** 
     * MD5数字签名 (小)
     * @param src 
     * @return 
     * @throws Exception 
     */  
    public static String md5Small(String src) throws Exception {  
       // 定义数字签名方法, 可用：MD5, SHA-1  
       MessageDigest md = MessageDigest.getInstance("MD5");
       byte[] b = md.digest(src.getBytes(UTF8));  
       return byte2MinStr(b);  
    }
      
    /** 
     * BASE64编码
     * @param src 
     * @return 
     * @throws Exception 
     */  
    public static String base64Encoder(String src) throws Exception {  
        BASE64Encoder encoder = new BASE64Encoder();  
        return encoder.encode(src.getBytes(UTF8));  
    }  
      
    /** 
     * BASE64解码
     * @param dest 
     * @return 
     * @throws Exception 
     */  
	public static String base64Decoder(String dest) throws Exception {  
        BASE64Decoder decoder = new BASE64Decoder();  
        return new String(decoder.decodeBuffer(dest), UTF8);  
    }  
      
    /** 
     * 字节数组转化为大写16进制字符串 
     * @param b 
     * @return 
     */  
    private static String byte2HexStr(byte[] b) {  
        StringBuilder sb = new StringBuilder();  
        for (int i = 0; i < b.length; i++) {  
            String s = Integer.toHexString(b[i] & 0xFF);  
            if (s.length() == 1) {  
                sb.append("0");  
            }  
            sb.append(s.toUpperCase());  
        }  
        return sb.toString();  
    }  
    /** 
     * 字节数组转化为小写16进制字符串 
     * @param b 
     * @return 
     */
    private static String  byte2MinStr(byte[] b){
    	StringBuffer sb = new StringBuffer();
	    for (int i = 0; i < b.length; i++) {
	        int val = ((int) b[i]) & 0xff;
	        if (val < 16){
	            sb.append("0");
	        }
	        sb.append(Integer.toHexString(val));
	    }
	    return sb.toString();
    }
    
    /**
	 * MD5加密 生成16位md5码
	 * @param inStr 待加密字符串
	 * @return 返回16位md5码
	 * @throws Exception
	 */
    public static String md5Encode16(String inStr) throws Exception {
        MessageDigest md5 = MessageDigest.getInstance("MD5");

        byte[] byteArray = inStr.getBytes("UTF-8");
        byte[] md5Bytes = md5.digest(byteArray);
        StringBuffer hexValue = new StringBuffer();
        for (int i = 0; i < md5Bytes.length; i++) {
            int val = ((int) md5Bytes[i]) & 0xff;
            if (val < 16) {
                hexValue.append("0");
            }
            hexValue.append(Integer.toHexString(val));
        }
        return hexValue.toString().substring(8, 24);
    }
}  

